<template>
  <div class="main user-layout-login">
    <!-- 输入框 -->
    <p class="title display-center">您正在找回密码</p>
    <div class="tab-box">
      <span
        class="tab"
        :class="tabCurrent==1?'tab-active':''"
        @click="tabClick(1)"
      >邮箱</span>
      <span>/</span>
      <span
        class="tab"
        :class="tabCurrent==2?'tab-active':''"
        @click="tabClick(2)"
      >手机</span>
    </div>
    <a-form-model
      ref="form"
      :model="form"
      :rules="rules"
      class="form-model"
    >
      <a-form-model-item
        prop="name"
        v-if="tabCurrent==1"
      >
        <a-input
          size="large"
          type="text"
          placeholder="邮箱"
          v-model="form.account"
        >
          <a-icon
            slot="prefix"
            type="user"
            :style="{ color: 'rgba(0,0,0,.25)' }"
          />
        </a-input>
      </a-form-model-item>

      <a-form-model-item
        prop="account"
        v-if="tabCurrent==2"
      >
        <a-input-group compact>
          <NationCodeSelect
            v-model="form.mobileAreaCode"
            style="width: 24%;height:40px"
          />
          <a-input
            v-model="form.account"
            style="width: 76%;height:40px"
          />
        </a-input-group>
      </a-form-model-item>
      <a-form-model-item prop="kaptcha">
        <div class="code-img-box">
          <a-input
            size="large"
            type="text"
            placeholder="图形验证码"
            v-model="form.kaptcha"
          >
            <a-icon
              slot="prefix"
              type="user"
              :style="{ color: 'rgba(0,0,0,.25)' }"
            />
          </a-input>
          <div
            class="code-img"
            @click="getFn"
          >
            <img
              :src="kaptcha.image"
              alt="图形验证码"
            >
          </div>
        </div>
      </a-form-model-item>
      <a-form-model-item prop="verifyCode">
        <div class="flex-justify-between ">
          <a-input
            style="width: 60%"
            size="large"
            placeholder="验证码"
            v-model="form.verifyCode"
          >
            <a-icon
              slot="prefix"
              type="safety"
              :style="{ color: 'rgba(0,0,0,.25)' }"
            />
          </a-input>
          <a-button
            style="width: 30%"
            size="large"
            type="primary"
            htmlType="submit"
            :loading="state.codeloading"
            :disabled="state.code"
            @click="sendVerifyCodeFn"
          >{{state.codemsg}}</a-button>
        </div>
      </a-form-model-item>
      <a-form-model-item>
        <a-input-password
          size="large"
          placeholder="设置新密码"
          v-model="form.password"
          @input="inputPassword"
        >
          <a-icon
            slot="prefix"
            type="lock"
            :style="{ color: 'rgba(0,0,0,.25)' }"
          />
        </a-input-password>
      </a-form-model-item>
      <a-form-model-item style="margin-top:40px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
          @click="handleSubmit"
        >提交</a-button>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>

<script>
import md5 from "md5";
import { mapActions } from "vuex";

import { timeFix } from "@/utils/util";
import { verifyCode } from "@/api/login";

import { ACCESS_TOKEN, USER_INFO } from "@/store/mutation-types";
import storage from "store";

import NationCodeSelect from "@/components/Select/nationCodeSelect";
import { get } from "@/api/kaptcha";
import { sendVerifyCode } from "@/api/sms";
import * as email from "@/api/email";
import { saveResetLoginVerifyCode } from "@/api/userPossword";
import { passwordLevel } from "@/utils/index";
import Image from "@/components/upload/image";
export default {
  components: {
    Image,
    NationCodeSelect,
  },
  data() {
    return {
      tabCurrent: 1,
      customActiveKey: "tab1",
      loginBtn: false,
      // login type: 0 email, 1 account, 2 telephone
      loginType: 0,
      isLoginError: false,
      requiredTwoStepCaptcha: false,
      stepCaptchaVisible: false,
      form: this.$form.createForm(this),
      state: {
        time: 60,
        loginBtn: false,
        // login type: 0 email, 1 account, 2 telephone
        loginType: 0,
        smsSendBtn: false,

        codeloading: false, //验证码loadding
        code: false, //验证码按钮禁用
        codemsg: "获取验证码",
        timeId: "",
      },

      kaptchaLoadding: false, //图形验证码loadding
      kaptcha: {},
      // 图形验证码信息
      form: {
        account: "", //账号
        password: "", //	密码，md5加密
        verifyCode: "", //手机验证密码
        mobileAreaCode: "+86",

        kaptcha: "", //图形验证码
        pwdSecurityLevel: 0, //密码级别 密码安全级别（0弱 1中 2强）
      },
      rules: {
        account: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        verifyCode: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        kaptcha: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getFn();
  },
  methods: {
    inputPassword(e) {
      let val = e.target.value;
      if (val.length >= 6) {
        this.form.pwdSecurityLevel = passwordLevel(val);
      }
    },
    tabClick(i) {
      this.tabCurrent = i;
    },
    ...mapActions(["Login", "Logout"]),

    // 登录
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.state.loginBtn = true;
          const loginParams = { ...this.form };
          loginParams.password = md5(this.form.password);
          this.saveResetLoginVerifyCodeFn(loginParams);
        } else {
          return false;
        }
        this.state.loginBtn = false;
      });
    },
    async saveResetLoginVerifyCodeFn(params) {
      let res = await saveResetLoginVerifyCode({
        account: params.account,
        password: params.password,
        verifyCode: params.verifyCode,
        pwdSecurityLevel: params.pwdSecurityLevel,
        mobileAreaCode: params.mobileAreaCode,
      });
      if (res.code == 0) {
        this.$message.success(res.msg);
        this.$nav('/user/login')
      }
    },
    // 获取手机验证码
    async sendVerifyCodeFn() {
      console.log(this.form);
      if (this.tabCurrent == 2) {
        if (this.form.mobileAreaCode == "") {
          return false;
        }
      }
      if (this.form.account == "") {
        return false;
      }
      if (this.form.kaptcha == "") {
        return false;
      }
      this.state.codeloading = true;
      if (this.tabCurrent == 1) {
        let res = await email.sendVerifyCode({
          email: this.form.account,
          type: 2,
          imgCode: this.form.kaptcha,
          imgId: this.kaptcha.id,
        });
        if (res.code == 0) {
          this.state.code = true;
          this.$message.success("发送成功");
          this.setTimeoutFn();
        } else if (res.code == "100042") {
          this.getFn();
        }
      }
      if (this.tabCurrent == 2) {
        let res = await sendVerifyCode({
          mobileAreaCode: this.form.mobileAreaCode,
          mobile: this.form.account,
          type: 2,
          imgCode: this.form.kaptcha,
          imgId: this.kaptcha.id,
        });
        if (res.code == 0) {
          this.state.code = true;
          this.$message.success("发送成功");
          this.setTimeoutFn();
        } else if (res.code == "100042") {
          this.getFn();
        }
      }
      this.state.codeloading = false;
    },
    // 倒计时
    setTimeoutFn() {
      let time = this.state.time;
      this.state.timeId = setInterval(() => {
        this.state.codemsg = `${time--}秒后重试`;
        if (time < 1) {
          this.state.codemsg = "获取验证码";
          this.state.code = false;
          clearInterval(this.state.timeId);
        }
      }, 1000);
    },
    // 获取图形验证码
    async getFn() {
      this.kaptchaLoadding = true;
      let res = await get();
      if (res.code == 0) {
        this.kaptcha = res.data;
        this.kaptchaLoadding = false;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.user-layout-login {
  padding-top: 20px;
  label {
    font-size: 14px;
  }
  .form-model {
    margin-top: 20px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 46px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
  .title {
    color: #1b1b1b;
    font-size: 24px;
    font-weight: normal;
    // margin-top: 20px;
    margin-bottom: 10px;
  }
  .tab-box {
    display: flex;
    align-items: flex-end;
    .tab {
      // float: left;
      // height: 30px;
      // line-height: 30px;
      font-size: 16px;
      color: #777777;
      cursor: pointer;
    }
    .tab-active {
      // float: left;
      // height: 30px;
      // line-height: 30px;
      font-size: 20px;
      color: @primary-color;
    }
  }
  .code-img-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .code-img {
      width: 200px;
      height: 40px;
      margin-left: 50px;
      border-radius: 4px;
      // border: 1px solid #eee;
      cursor: pointer;
      img {
        width: 100%;
        height: 40px;
        border-radius: 4px;
      }
    }
  }
}
</style>
